﻿namespace PayPal.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using PayPal;
    using PayPal.ButtonManager;

    [TestClass]
    public class ButtonManagerTests
    {
        public ButtonManagerTests()
        {
            PayPal.Profile.Initialize(
                            "msft_1284494024_biz_api1.yahoo.com",
                            "1284494030",
                            "AXmPHOGl57wrwF1dy49VI0W9gisLAWm4raYoWQy9KHCtpd7kLlSQcsFG",
                            "sandbox");
        }

        [TestMethod]
        public void TestBuyNowButtonWithSimpleParameters()
        {
            var response = BuyNowButton.Create("msft_1284494024_biz@yahoo.com", "Wireless Mouse", "11.50");

            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestBuyNowButtonDelete()
        {
            var button = BuyNowButton.Create("msft_1284494024_biz@yahoo.com", "Wireless Mouse", "11.50");

            Assert.IsNotNull(button.HostedButtonId);

            BuyNowButton.Delete(button.HostedButtonId);
        }

        [TestMethod]
        [Ignore] //// Update operation of Button Manager API is currently not working
        public void TestBuyNowButtonUpdateWithSimpleParameters()
        {
            var response = BuyNowButton.Create(
                                            business: "msft_1284494024_biz@yahoo.com", 
                                            itemName: "Wireless Mouse",
                                            amount: "11.50");

            Assert.IsNotNull(response.HostedButtonId);

            string buttonId = response.HostedButtonId;

            response = BuyNowButton.Update(
                    hostedButtonId: buttonId,
                    business: "msft_1284494024_biz@yahoo.com",
                    itemName: "Wireless Mouse (Updated)",
                    amount: "200.00");

            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestBuyNowButtonWithMultipleOptions()
        {
            var buttonVariables = new HtmlButtonVariables
            {
                Business = "msft_1284494024_biz@yahoo.com",
                ItemName = "Wireless Mouse",
                CurrencyCode = "USD"
            };

            var response = BuyNowButton.Create(
                                            buttonVariables, 
                                            ButtonOptions.Create(
                                                ButtonOption.Create(
                                                    "color",
                                                    ButtonOptionItems.Create(
                                                        ButtonOptionItem.Create("Red", "11.50"),
                                                        ButtonOptionItem.Create("Blue", "12.50")))));
            
            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestAddToCartButtonWithSimpleParamters()
        {
            var response = AddToCartButton.Create("msft_1284494024_biz@yahoo.com", "Wireless Mouse", "199.99");

            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestAddToCartButtonWithMultipleOptions()
        {
            var buttonVariables = new HtmlButtonVariables
            {
                Business = "msft_1284494024_biz@yahoo.com",
                ItemName = "Wireless Mouse",
                CurrencyCode = "USD"
            };

            var response = AddToCartButton.Create(
                                            buttonVariables,
                                            ButtonOptions.Create(
                                                ButtonOption.Create(
                                                    "color",
                                                    ButtonOptionItems.Create(
                                                        ButtonOptionItem.Create("Red", "11.50"),
                                                        ButtonOptionItem.Create("Blue", "12.50")))));

            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestAddToCartButtonDelete()
        {
            var button = AddToCartButton.Create("msft_1284494024_biz@yahoo.com", "Wireless Mouse", "199.99");

            Assert.IsNotNull(button.HostedButtonId);

            AddToCartButton.Delete(button.HostedButtonId);
        }

        [TestMethod]
        [Ignore] //// Update operation of Button Manager API is currently not working
        public void TestAddToCartButtonUpdateWithSimpleParameters()
        {
            var response = AddToCartButton.Create(
                                                business: "msft_1284494024_biz@yahoo.com",
                                                itemName: "Wireless Mouse",
                                                amount: "199.99");

            Assert.IsNotNull(response.HostedButtonId);

            string buttonId = response.HostedButtonId;

            response = AddToCartButton.Update(
                    hostedButtonId: buttonId,
                    business: "msft_1284494024_biz@yahoo.com",
                    itemName: "Wireless Mouse (Updated)",
                    amount: "200.00");

            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestSubscribeButtonWithSimpleParameters()
        {
            var response = SubscribeButton.Create("msft_1284494024_biz@yahoo.com", "Magazine Subscription", "10.00", "2", "M");

            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestSubscribeButtonWithMultipleOptions()
        {
            var buttonVariables = new HtmlButtonVariables
            {
                Business = "msft_1284494024_biz@yahoo.com",
                ItemName = "Magazine Subscription",
                CurrencyCode = "USD",
                A3 = "10.00", // a3 - amount to billed each recurrence
                P3 = "2", // p3 - number of time periods between each recurrence
                T3 = "M" // t3 - time period (D=days, W=weeks, M=months, Y=years)
            };

            var response = SubscribeButton.Create(
                                        buttonVariables,
                                        ButtonOptions.Create(
                                            ButtonOption.Create(
                                                    "Plan",
                                                    ButtonOptionItems.Create(
                                                        ButtonOptionItem.Create(name: "Regular", price: "10.00", billingPeriod: "Day", billingFrequency: 3),
                                                        ButtonOptionItem.Create(name: "Super", price: "15.00", billingPeriod: "Day", billingFrequency: 4),
                                                        ButtonOptionItem.Create(name: "Premium", price: "20.00", billingPeriod: "Day", billingFrequency: 5)))));
            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestSubscribeButtonUpdateWithSimpleParameters()
        {
            var response = SubscribeButton.Create(
                                            business: "msft_1284494024_biz@yahoo.com",
                                            itemName: "Magazine Subscription",
                                            a3: "10.00",
                                            p3: "2",
                                            t3: "M");

            Assert.IsNotNull(response.HostedButtonId);

            string buttonId = response.HostedButtonId;

            response = SubscribeButton.Update(
                    hostedButtonId: buttonId,
                    business: "msft_1284494024_biz@yahoo.com",
                    itemName: "Magazine Subscription (Updated)",
                    a3: "15.00",
                    p3: "3",
                    t3: "W");

            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestSubscribeButtonUpdateWithMultipleOptions()
        {
            var response = SubscribeButton.Create(
                                            business: "msft_1284494024_biz@yahoo.com",
                                            itemName: "Magazine Subscription",
                                            a3: "10.00",
                                            p3: "2",
                                            t3: "M");

            Assert.IsNotNull(response.HostedButtonId);

            string buttonId = response.HostedButtonId;

            var buttonVariables = new HtmlButtonVariables
            {
                Business = "msft_1284494024_biz@yahoo.com",
                ItemName = "Magazine Subscription (Updated)",
                CurrencyCode = "USD",
                A3 = "15.00", // a3 - amount to billed each recurrence
                P3 = "3", // p3 - number of time periods between each recurrence
                T3 = "W" // t3 - time period (D=days, W=weeks, M=months, Y=years)
            };

            response = SubscribeButton.Update(
                                        buttonId,
                                        buttonVariables,
                                        ButtonOptions.Create(
                                            ButtonOption.Create(
                                                    "Plan",
                                                    ButtonOptionItems.Create(
                                                        ButtonOptionItem.Create(name: "Regular", price: "10.00", billingPeriod: "Day", billingFrequency: 3),
                                                        ButtonOptionItem.Create(name: "Super", price: "15.00", billingPeriod: "Day", billingFrequency: 4),
                                                        ButtonOptionItem.Create(name: "Premium", price: "20.00", billingPeriod: "Day", billingFrequency: 5)))));
            
            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestSubscribeButtonDelete()
        {
            var button = SubscribeButton.Create("msft_1284494024_biz@yahoo.com", "Magazine Subscription", "10.00", "2", "M");

            Assert.IsNotNull(button.HostedButtonId);

            SubscribeButton.Delete(button.HostedButtonId);
        }

        [TestMethod]
        public void TestDonateButtonWithSimpleParameters()
        {
            var response = DonateButton.Create("msft_1284494024_biz@yahoo.com", "Charity Event");

            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestDonateButtonWithMultipleOptions()
        {
            var buttonVariables = new HtmlButtonVariables
            {
                Business = "msft_1284494024_biz@yahoo.com",
                ItemName = "Charity Event",
                CurrencyCode = "USD"
            };

            var response = DonateButton.Create(
                                            buttonVariables,
                                            ButtonOptions.Create(
                                                ButtonOption.Create(
                                                        "Location",
                                                        ButtonOptionItems.Create(
                                                            ButtonOptionItem.Create(name: "New York"),
                                                            ButtonOptionItem.Create(name: "Seattle")))));

            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestDonateButtonUpdateWithSimpleParameters()
        {
            var response = DonateButton.Create("msft_1284494024_biz@yahoo.com", "Charity Event");

            Assert.IsNotNull(response.HostedButtonId);

            string buttonId = response.HostedButtonId;

            response = DonateButton.Update(buttonId, "msft_1284494024_biz@yahoo.com", "Charity Event (Updated)");
        }

        [TestMethod]
        public void TestDonateButtonUpdateWithMultipleOptions()
        {
            var response = DonateButton.Create("msft_1284494024_biz@yahoo.com", "Charity Event");

            Assert.IsNotNull(response.HostedButtonId);

            string buttonId = response.HostedButtonId;

            var buttonVariables = new HtmlButtonVariables
            {
                Business = "msft_1284494024_biz@yahoo.com",
                ItemName = "Charity Event (updated)",
                CurrencyCode = "USD"
            };

            response = DonateButton.Update(
                                buttonId,
                                buttonVariables,
                                ButtonOptions.Create(
                                    ButtonOption.Create(
                                            "Location",
                                            ButtonOptionItems.Create(
                                                ButtonOptionItem.Create(name: "New York"),
                                                ButtonOptionItem.Create(name: "Seattle")))));

            Assert.IsNotNull(response.WebSiteCode);
            Assert.IsNotNull(response.HostedButtonId);
        }

        [TestMethod]
        public void TestDonateButtonDelete()
        {
            var button = DonateButton.Create("msft_1284494024_biz@yahoo.com", "Charity Event");

            Assert.IsNotNull(button.HostedButtonId);

            DonateButton.Delete(button.HostedButtonId);
        }
    }
}
